<?php

defined('SYSPATH') or die('No direct access allowed.');

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

class Domain_Entities_Generic {

     /**
     *
     * @param <type> $name
     * @param <type> $value
     */
    public function __set($name, $value)
    { 
        if (property_exists($this, $name) || $name[0] . $name[1] == "__")
        {
            $this->$name = $value;
        }
    }

    /**
     *
     * @param <type> $name
     * @return <type>
     */
    public function __get($name)
    {
        if (property_exists($this, $name) || ($name[0] . $name[1] == "__" && isset($this->$name)))
        {
            $value = $this->$name;
            return $value === null?'':$value;
        }
        return null;
    }

    /**
     * just to build function setParame, getParame
     * @param <type> $name
     * @param <type> $arguments
     * @return <type>
     */
    public function __call($name, $arguments) {
        $s_action = strtolower(substr($name, 0, 3));
        $s_fun = strtolower(substr($name, 3));
        //for action getParame
        if ($s_action === "get") {
            $value = $this->__get($s_fun);
            if(is_string($value) && count($arguments) > 0 && $arguments[0]){
                return HTML::chars($value);
            }
            
            return $value;
        }
        //for action setParame
        else if ($s_action === "set") {
            $this->$s_fun = $arguments[0];
            return $this;
        }
    }

    /**
     * get string of class
     * @return <String>
     */
    public function __toString()
    {
        return get_class($this);
    }

    /**
     * get all column of table in this Object
     * @return <Array>
     */
    public function to_column_array()
    {
        $a_class = get_class_vars($this);
        $a_columns = array();
        foreach ($a_class as $key => $vaule) {
            if ($key[0] != "_" && $key[0] . $key[1] != "__")
            {
                $a_columns[$key] = $key;
            }
        }
        return $a_columns;
    }

    /**
     * 2 word begin string is column alias
     * get all alias of data in this Object(ex: begin __alias)
     * @return <Array>
     */
    public function to_column_ext_array()
    {
        $a_class = get_class_vars($this);
        $a_columns = array();
        foreach ($a_class as $key => $vaule) {
            if ($key[0] . $key[1] == "__")
            {
                $a_columns[$key] = $key;
            }
        }
        return $a_columns;
    }

    /**
     * get all array of Object(foreign_key) in this Object (ex: _table)
     * @return <Array>
     */
    public function to_table_array()
    {
        $a_class = get_class_vars($this);
        $a_tables = array();
        foreach ($a_class as $key => $vaule) {
            if ($key[0] == "_" && $key[0] . $key[1] != "__")
            {
                $a_tables[$key] = $key;
            }
        }
        return $a_tables;
    }

    /**
     * get column of table(foreign_key) in this Object (have 3 char end is _id)
     * @return <Array>
     */
    public function to_foreign_key_array()
    {
        $a_class = get_class_vars($this);
        $a_columns = array();
        foreach ($a_class as $key => $vaule) {
            if (strlen($key) >= 4)
            {
                if ($key[strlen($key) - 3] . $key[strlen($key) - 2] . $key[strlen($key) - 1] == "_id")
                {
                    $a_columns[$key] = $key;
                }
            }
        }
        return $a_columns;
    }

    /**
     * clear_data_foreign_key
     */
    public function clear_data_foreign_key()
    {
        $a_table = $this->to_table_array();
        if (!empty($a_table))
        {
            foreach ($a_table as $key => $value) {
                $this->$key = array();
            }
        }
    }

    /**
     *
     * @param <type> $a_data
     * @return <type> 
     */
    public function set_array_to_object($a_data)
    {
        try {
            foreach ($a_data as $key => $value) {
                $s_key = 'set' . $key;
                $this->$s_key($value);
            }
        } catch (Exception $e) {
            Log::instance()->add(Log::ERROR, 'set_array_to_object($a_data): ' . $e);
            Log::instance()->write();
            return null;
        }
    }

    public function as_array()
    {
        $a_r = array();
        $a_column = $this->to_column_array();
        if (!empty($a_column))
        {
            foreach ($a_column as $key => $value) {
                $a_r[$key] = $this->$key;
            }
        }
        $a_column = $this->to_column_ext_array();
        if (!empty($a_column))
        {
            foreach ($a_column as $key => $value) {
                $a_r[$key] = $this->$key;
            }
        }
        return $a_r;
    }

}

?>
